package se.thoka.twt.client.utils.validation;

public class DoubleValidation implements Validation {
	
	public static final DoubleValidation INSTANCE_ALLOW_NULL = new DoubleValidation(true);
	public static final DoubleValidation INSTANCE_NOT_NULL = new DoubleValidation(false);
	
	private final boolean allowNull;
	
	public DoubleValidation(boolean allowNull) {
		this.allowNull = allowNull;
	}

	@Override
	public String getDescription() {
		return ValidationUtil.getStrings().invalidInteger();
	}

	@Override
	public boolean validate(String value) {
		boolean valid;
		if (value != null) {
			try {
				Double.parseDouble(value);
				valid = true;
			} catch (NumberFormatException e) {
				valid = false;
			}
		} else {
			valid = allowNull;
		}
		
		return valid;
	}
}
